﻿using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace IGS.Core.Tests.Integrators
{
    internal static class RateOfConvergenceAnalysis
    {
        public static void AssertConvergence(List<double> deltas, double rateOfConvergence)
        {
            var powers = new List<double>();
            for (var n = 0; n < deltas.Count - 1; n++)
            {
                var mu = deltas[n] / deltas[n + 1];
                var power = Math.Log(mu, rateOfConvergence);
                powers.Add(power);
            }

            var product = 1.0 * (powers[1] / powers[0]);
            for (var i = 2; i < powers.Count; i++)
            {
                var ratio = powers[i] / powers[i - 1];

                var lastMean = Math.Pow(product, i - 1);
                product *= ratio;
                var currentMean = Math.Pow(product, i);

                Assert.Greater(currentMean, lastMean, "Failed to converge with order = {0}", rateOfConvergence);
            }
        }
    }
}
